import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserModule } from './modules/user/user.module';
import { PaginationService } from './common/pagination/pagination.service';
import { AuthModule } from './modules/auth/auth.module';
import { PaginationModule } from './common/pagination/pagination.module';
import configuration from './configuration';
import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from './modules/auth/auth.guard';
import { ClassificationModule } from './modules/classification/classification.module';
import { ArticleModule } from './modules/article/article.module';
import { TagModule } from './modules/tag/tag.module';
import { RoleModule } from './modules/role/role.module';
import { PermissionModule } from './modules/permission/permission.module';
import { PermissionGuard } from './modules/permission/permission.guard';
import { FileModule } from './modules/file/file.module';
//导入MulterModule，用来在NestJS里注册模块
import { MulterModule } from '@nestjs/platform-express';
import { storage } from './modules/file/storage';
import { SystemModule } from './modules/system/system.module';
import { UserVisitModule } from './modules/user-visit/user-visit.module';


@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      cache: true,
      load: [configuration],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          type: 'mysql',
          connectorPackage: 'mysql2',
          host: configService.get('db.mysql.host'),
          port: +configService.get('db.mysql.port'),
          username: configService.get('db.mysql.username'),
          password: configService.get('db.mysql.password'),
          database: configService.get('db.mysql.database'),
          entities: [__dirname + '/**/*.entity{.ts,.js}'],
          logging: false,
          synchronize: true,
          autoLoadEntities: true,
        };
      },
    }),
    //在Module的imports数组里，注册并配置Multer模块
    //调用MulterModule的register()方法进行配置，传入配置对象
    MulterModule.register({
      //配置项的storage属性决定文件的储存位置和方式
      //通过multer模块的diskStorage来进行配置，表示文件存入磁盘，传入配置对象
      storage,
    }),
    AuthModule,
    UserModule,
    RoleModule,
    PermissionModule,
    PaginationModule,
    ClassificationModule,
    ArticleModule,
    TagModule,
    FileModule,
    SystemModule,
    UserVisitModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    PaginationService,
    // 注册为全局守卫
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
    {
      provide: APP_GUARD,
      useClass: PermissionGuard,
    },
  ],
})
export class AppModule {}
